package com.nic.view.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.nic.view.widget.CalendarViewDialog;
import com.nic.view.widget.CustomDatePicker;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2018/11/6.
 */

public abstract class VBaseFragment extends Fragment {
    public Activity activity;
    public View rootView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = LayoutInflater.from(activity).inflate(getContentLayoutId(), container, false);
        initPreviewData(rootView);
        initView(rootView);
        initViewData(rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public abstract int getContentLayoutId();

    public abstract void initView(View rootView);

    public void initPreviewData(View rootView) {
    }


    public void initViewData(View rootView) {
    }

    public void showToastSHORT(String title) {
        Toast.makeText(activity, title, Toast.LENGTH_SHORT).show();
    }

    /**
     * 日期
     *
     * @param editText
     */
    public void showCustomerDatePopWindow(final TextView editText, CustomDatePickerType datePickerType) {
        showCustomerDatePopWindow(editText, datePickerType, false);
    }

    /**
     * 日期
     *
     * @param editText
     */
    public void showCustomerDatePopWindow(final TextView editText, final CustomDatePickerType datePickerType, final boolean isShowHourAndMinute) {
        try {
            final String pattern = "yyyy-MM-dd HH:mm";
            final String pattern1 = "yyyy-MM-dd";
            final String pattern2 = "yyyy-MM-dd HH:mm:ss";
            String selectedDate = editText.getText().toString();
            if (TextUtils.isEmpty(selectedDate)) {
                if (isShowHourAndMinute) {
                    SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.CHINA);
                    selectedDate = sdf.format(new Date());
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat(pattern1, Locale.CHINA);
                    selectedDate = sdf.format(new Date());
                }

            }

            CustomDatePicker datePicker = new CustomDatePicker(activity, new CustomDatePicker.ResultHandler() {
                @Override
                public void handle(String time) {
                    if (datePickerType != CustomDatePickerType.ALL) {
                        try {
                            Date selectDateTime = new SimpleDateFormat(pattern).parse(time);
                            Calendar selectC = Calendar.getInstance();
                            selectC.setTime(selectDateTime);
                            int selectYear = selectC.get(Calendar.YEAR);
                            int selectMonth = selectC.get(Calendar.MONTH);
                            int selectDay = selectC.get(Calendar.DAY_OF_MONTH);
                            int selectHour = selectC.get(Calendar.HOUR_OF_DAY);
                            int selectMinute = selectC.get(Calendar.MINUTE);
                            Calendar c = Calendar.getInstance();
                            int year = c.get(Calendar.YEAR);
                            int month = c.get(Calendar.MONTH);
                            int day = c.get(Calendar.DAY_OF_MONTH);
                            int hour = c.get(Calendar.HOUR_OF_DAY);
                            int minute = c.get(Calendar.MINUTE);
                            if (datePickerType == CustomDatePickerType.FUTURE) {
                                if (selectYear < year) {
                                    showToastSHORT("不能低于当前年，请重新选择");
                                    return;
                                }
                                if (selectYear == year && selectMonth < month) {
                                    showToastSHORT("不能低于当月份，请重新选择");
                                    return;
                                }
                                if (selectYear == year && selectMonth == month && selectDay < day) {
                                    showToastSHORT("不能低于当日期，请重新选择");
                                    return;
                                }
                                if (isShowHourAndMinute) {
                                    if (selectYear == year && selectMonth == month && selectDay == day && selectHour < hour) {
                                        showToastSHORT("不能低于当时间，请重新选择");
                                        return;
                                    }
                                    if (selectYear == year && selectMonth == month && selectDay == day && selectHour == hour && selectMinute < minute) {
                                        showToastSHORT("不能低于当时间，请重新选择");
                                        return;
                                    }
                                }
                            }
                            if (datePickerType == CustomDatePickerType.HISTORY) {
                                if (selectYear > year) {
                                    showToastSHORT("不能超出当前年，请重新选择");
                                    return;
                                }
                                if (selectYear == year && selectMonth > month) {
                                    showToastSHORT("不能超出当月份，请重新选择");
                                    return;
                                }
                                if (selectYear == year && selectMonth == month && selectDay > day) {
                                    showToastSHORT("不能超出当日期，请重新选择");
                                    return;
                                }
                                if (isShowHourAndMinute) {
                                    if (selectYear == year && selectMonth == month && selectDay == day && selectHour > hour) {
                                        showToastSHORT("不能超出当时间，请重新选择");
                                        return;
                                    }
                                    if (selectYear == year && selectMonth == month && selectDay == day && selectHour == hour && selectMinute > minute) {
                                        showToastSHORT("不能超出当时间，请重新选择");
                                        return;
                                    }
                                }
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    if (isShowHourAndMinute) {
                        editText.setText(time);
                    } else {
                        editText.setText(time.split(" ")[0]);
                    }

                }
            });
            datePicker.showSpecificTime(isShowHourAndMinute);
            datePicker.show(selectedDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public enum CustomDatePickerType {
        HISTORY,
        FUTURE,
        ALL
    }


    /**
     * 日期
     *
     * @param context
     * @param editText
     */
    public void showDatePopWindow(Context context, final TextView editText, final boolean isHistory) {
        try {
            final CalendarViewDialog mCalendarViewDialog = new CalendarViewDialog(context, CalendarViewDialog.CORRECT_TO_DAY_1ST);
            mCalendarViewDialog.setOnBtnClickListener(new CalendarViewDialog.OnBtnClickListener() {
                @Override
                public void onClick(View v) {
                    String selectTime = mCalendarViewDialog.getCalendarDate("yyyy-MM-dd");
                    if (isHistory) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            if (!new Date().after(format.parse(selectTime))) {
                                showToastSHORT("不能大于当前时间，请重新选择");
                                return;
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    editText.setText(selectTime);
                }
            });
            mCalendarViewDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showDatePopWindow(final TextView editText, final boolean isHistory) {
        showDatePopWindow(activity, editText, isHistory);
    }
}
